import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/regional_organization_tree/regional_organization_tree_view.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/customEndDrawer.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/common/risk_tree_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/double_control/instrument_ledger_count_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/double_control/instrumnet_ledger_list_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/double_prevention_page/risk_control_page/risk_control_controller.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';

///仪表监测台账
class InstrumentMonitoringLedgerPageController extends GetxController {
  /// 搜索字符串
  String? searchX = '';

  /// 列表刷新key
  final GlobalKey<CustomEasyRefreshListState> globalKey = GlobalKey();

  /// 选中的节点
  RiskTreeEntity selectedEntity = RiskTreeEntity();

  /// 筛选右侧栏控制Key
  final riskRightScaffoldKey = GlobalKey<ScaffoldState>();
  InstrumentLedgerCountEntity? data;

  @override
  void onInit() {
    super.onInit();
    count();
  }

  ///统计
  void count() async {
    final res = await HttpUtil().generateGetRequest<InstrumentLedgerCountEntity>(
      Interface.getDataPageCount,
      queryParameters: {
        'keywords': searchX,
        'ids': selectedEntity.id, //当前层级的id、对象 单元 事件 措施的id 默认全部 // 需要筛选后的id填入
        'level': selectedEntity.level, //默认 1
      },
    ).execute();
    res.onSuccess((result) {
      if (result.data != null) {
        data = result.data!;
      }
    });
    update();
  }

  /// 获取任务执行台帐分页列表
  Future<List<InstrumnetLedgerListEntity>> getDataPage(int index, int size, {String? search}) async {
    final res = await HttpUtil().generateGetRequest<List<InstrumnetLedgerListEntity>>(
      Interface.getDataPage,
      queryParameters: {
        'pageNum': index,
        'pageSize': size,
        'keywords': searchX,
        'parentIds': selectedEntity.id, //当前层级的id、对象 单元 事件 措施的id 默认全部 // 需要筛选后的id填入
        'level': selectedEntity.level, //默认 1
      },
    ).execute();
    List<InstrumnetLedgerListEntity>? list = [];
    res.onSuccess((result) {
      if (result.data != null) {
        list = result.data;
      }
    });
    return list!;
  }

  /// 筛选按钮
  void onFilterButtonPressed() {
    final RiskControlController controller = Get.find<RiskControlController>();
    controller.filterWidget.value = _filterWidget();
    controller.riskRightScaffoldKey.currentState?.openEndDrawer();
    // riskRightScaffoldKey.currentState?.openEndDrawer();
  }

  /// 重置
  void reset() {
    final RiskControlController controller = Get.find<RiskControlController>();
    controller.riskRightScaffoldKey.currentState?.closeEndDrawer();
    selectedEntity = RiskTreeEntity();
    globalKey.currentState!.onRefresh();
    count();
  }

  /// 确定
  void confirm() {
    Get.back();
    globalKey.currentState!.onRefresh();
    count();
  }

  /// 详情
  void clickDetail() {
    Get.toNamed(AppRoutes.instrumentMonitoringLedgerDetailPage);
  }

  Widget _filterWidget() {
    return CustomEndDrawer(
      centerContext: Column(
        children: [
          RegionalOrganizationTreePage(
            selectedEntity: selectedEntity,
            onCallBack: (RiskTreeEntity entity) {
              selectedEntity = entity;
            },
          ),
        ],
      ),
      reset: reset,
      confirm: confirm,
    );
  }
}
